package com.dao;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSON;
import com.servlet.My;

import hyl.core.MyFun;
import hyl.core.db.MyDB;
import hyl.core.db.MySqlSelect;
import hyl.core.db.MySqlSelect2;
import hyl.core.info.Content;

public class SqlOfNews {

	// hzp 添加新闻
	public static void add新闻(String uname, String title, String text, String state, int rate, String type, String arr) {

		MyDB db = My.getDB1();
		String sql = "select id from user where uname=?";
		Object[] param = { uname };
		Map<String, Object> map = db.queryMap(sql, param);
		db = My.getDB1();
		sql = "insert into sys_html(uid,ctime,etime,ftime,title,t1,state,oid,i3)values(?,now(),now(),now(),?,?,?,?,?)";
		Object[] param1 = { map.get("id"), title, text, state, rate, type };
		long sid = db.executeUpdate(sql, param1);

		MyDB db1 = My.createCommitDb();
		if (rate != 0) {
			String[] array = MyFun.str2Array(arr);
			for (String oid : array) {
				sql = "insert into org_html(sid,oid)values(?,?)";
				Object[] par = { sid, oid };
				db1.executeUpdate(sql, par);
			}
			db1.commit();
		}
	}

	public static String get新闻(String id) {

		MyDB db = My.getDB1();
		String sql = "select state,title,content,oid,t1 from sys_html where id=?";
		Object[] param = { id };
		Map<String, Object> map = db.queryMap(sql, param);
		sql = "select oid from org_html where sid=?";
		List<?> list = db.queryList(sql, param);
		map.put("arr", JSON.toJSONString(list));
		return JSON.toJSONString(map);
	}

	public static Integer 修改新闻(String id, String title, String text, String state, String rate, String type) {

		MyDB db = My.getDB1();
		String sql = "update sys_html set title=?,t1=?,state=?,etime=now(),oid=?,i3=? where id=?";
		Object[] param = { title, text, state, rate, type, id };
		db.executeUpdate(sql, param);
		return 1;
	}

	// HZP 2019-03-05 14:17
	public static Map<String, Object> get首页数据(String uname) {
		MyDB db = My.getDB1();
		String sql = "select a.l0,a.l1,a.l4,a.l5,c.l4 as i0,a.l7,a.l8 from act_finance a,user b,user_ext c where a.uid=b.id and b.id=c.id and b.uname=?";
		Object[] param = { uname };
		return db.queryMap(sql, param);
	}

	public static Map<String, Object> get新闻公告(int index, int size) {

		Content ct = new Content();
		MySqlSelect count = MySqlSelect.getInstance("select count(1) as total from sys_html a,user b where a.uid=b.id");
		String sql = "select a.ctime,a.title,b.ualias,a.state,a.id from sys_html a,user b where a.uid=b.id order by a.id desc {1}";
		MySqlSelect2 select = MySqlSelect2.getInstance(sql);

		select.fromto(1, index, size);
		ct.setCode(1, "查询成功");
		ct.iniBodys(count.getMap());
		ct.addBodyItem("_rs", select.getList2D());
		return ct.get();
	}

// hzp 添加新闻

}
